﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin_manageRole.aspx.cs" Inherits="scmsByAspdNet.Admin_manageRole" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>角色管理</title>
    <link rel="stylesheet" href="static/bootstrap-4.6.0/css/bootstrap.css" />
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.css" />
    <script src="static/js/jquery-3.6.0.js"></script>
    <script src="static/js/jquery-3.6.0.min.js"></script>
</head>
<body>
    <%-- 导航栏 --%>
    <div class="nav-warp">
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="Admin.aspx">学生选课系统-管理员界面</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">更多功能
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">相关信息</a>
                            <a class="dropdown-item" href="#">关于我们</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#">版本有关</a>
                        </div>
                    </li>
                </ul>
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item">
                        <a id="sign-out" class="nav-link" href="./Login.aspx" tabindex="-1" aria-disabled="true" style="color: red;">退出登录</a>
                    </li>
                </ul>
                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="search" placeholder="没有任何功能" aria-label="Search" />
                    <button class="btn btn-outline-success my-2 my-sm-0 loginSubmit" type="submit">Search</button>
                </form>
            </div>
        </nav>
    </div>
    <%-- 表格 --%>
    <div style="width:1200px ;margin: 0 auto">
        <table id="role-manage-table" style="max-width: 1200px;"">
        </table>
    </div>

</body>
<script src="static/bootstrap-4.6.0/js/bootstrap.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<script>
    let $role_manage_table;
    init_role_manage_table();
    function init_role_manage_table() {
        $role_manage_table = $('#role-manage-table').bootstrapTable({
            url: 'cotro/Admin/getUsers.ashx',
            method: "post",
            search: true, //是否显示表格搜索
            sidePagination: "client", //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,
            pageSize: 5, //每页的记录行数（*）
            pageList: [10, 25, 50, 100], //可供选择的每页的行数（*）
            pagination: true, //是否显示分页（*）
            height: 560,
            singleSelect: false,
            contentType: "application/x-www-form-urlencoded", //发送给服务器的编码类型
            columns: [{
                field: 'indiv_id',
                title: '用户号',
            }, {
                field: 'role',
                title: '用户角色',
                formatter: function (value, row, index) {
                    return role_manage_table_formatter(value, row, index, 'role')
                },
            }, {
                field: 'indiv_id',
                title: '可供操作',
                formatter: function (value, row, index) {
                    return role_manage_table_formatter(value, row, index, 'offer')
                },
            },]
        });
    };
    function role_manage_table_formatter(value, row, index, op) {
        let result = ``;
        if (op === "indiv_id") {
            result = `<input type="text" class="form-control" value=${value}>`
        }
        if (op === "role") {
            if (value == 1) {
                result = `<div class="form-group">
                        <select class="form-control indiv_role" >
                          <option value="1" selected = "selected">学生</option>
                          <option value="2" >教师</option>
                          <option value="3">管理员</option>
                        </select>
                      </div>`
            }
            else if (value == 2) {
                result = `<div class="form-group">
                        <select class="form-control indiv_role">
                          <option value="1" >学生</option>
                          <option value="2" selected = "selected">教师</option>
                          <option value="3">管理员</option>
                        </select>
                      </div>`
            }
            else if (value == 3) {
                result = `<div class="form-group">
                        <select class="form-control indiv_role" >
                          <option value="1" >学生</option>
                          <option value="2" >教师</option>
                          <option value="3" selected = "selected">管理员</option>
                        </select>
                      </div>`
            }
            else {
                result = `<div class="form-group">
                        <select class="form-control indiv_role" >
                          <option value="0" selected = "selected">无</option>
                          <option value="1" >学生</option>
                          <option value="2" >教师</option>
                          <option value="3" >管理员</option>
                        </select>
                      </div>`
            }
        }
        if (op === "offer") {
            result = `<button type="button" class="btn btn-danger" onclick="role_manage_op(${index},'confirm_modify',${value})">确认修改</button>`
        }
        return result;
    }
    function role_manage_op(...value) {
        if (value[1] === "confirm_modify") {
            indiv_id = value[2];
            indiv_role = document.getElementsByClassName("indiv_role")[value[0]].value;
            console.log(indiv_id)
            console.log(indiv_role)
            $.post("cotro/Admin/updateUsersRole.ashx", {
                indiv_id:indiv_id,
                indiv_role:indiv_role
            }, function (res, status) {
                res = JSON.parse(res);
                if (res.line == 1) {
                    alert("修改成功")
                } else {
                    alert("修改失败")
                }
            })
        }
    }
</script>
</html>
